import Axios from 'axios'
import * as types from './store/types'
// import request from 'umi-request';
import {
	extend
} from 'umi-request';
let errNumber = 0;
let dealError = function(error, that) {
	console.log(errNumber)
	that.$Loading.error();
	that.$Spin.hide();
	// let status=error.response.status;
	console.log("status", error)
	if (error && error.response) {
		let status = error.response.status;
		if (error.response.status == 403) { //forbiden
			if (errNumber == 1) {
				that.$Notice.error({
					title: '请求拒绝',
					desc: '您没有当前操作权限',
					onClose: function() {
						errNumber = 0
					}
				});
			}
		} else if (error.response.status == 401) { //forbiden
			if (errNumber == 1) {
				that.$Notice.error({
					title: '用户未登陆',
					desc: '请重新登陆该系统',
					onClose: function() {
						errNumber = 0
					}
				});
			}
		} else if (error.response.status == 409) { //forbiden
			if (errNumber == 1) {
				that.$Modal.warning({
					title: '重复登录提示：',
					content: "您已在其他地方登录！",
					width: '500',
					onOk: () => {
						errNumber = 0
						localStorage.clear();
						sessionStorage.clear();
						that.$store.commit(types.LOGOUT);
						that.$router.push({
							path: '/login'
						});
					}
				});
			}
		} else if (error.response.status == 400) { //forbiden
			if (errNumber == 1) {
				that.$Notice.error({
					title: '访问数据不存在',
					desc: '请联系管理员进行处理',
					onClose: function() {
						errNumber = 0
					}
				});
			}
		} else if (error.response.status == 406) { //forbiden
			if (errNumber == 1) {
				that.$Notice.error({
					desc: error.data ? error.data : error.response.data,
					duration: 5,
					onClose: function() {
						errNumber = 0
					}
				});
			}
		} else if (error.response.status == 429) { //forbiden
			if (errNumber == 1) {
				that.$Notice.error({
					title: '操作异常',
					desc: "操作太快，请稍后再试",
					duration: 5,
					onClose: function() {
						errNumber = 0
					}
				});
			}
		} else {
			if (errNumber == 1) {
				that.$Notice.error({
					title: '网络异常',
					desc: '请联系管理员进行处理',
					onClose: function() {
						errNumber = 0
					}
				});
			}
		}
	} else if (error == 400) {
		if (errNumber == 1) {
			that.$Notice.warning({
				title: '访问数据不存在',
				desc: '请联系管理员进行处理',
				onClose: function() {
					errNumber = 0
				}
			});
		}
	} else if (error == 403) { //forbiden
		if (errNumber == 1) {
			that.$Notice.error({
				title: '请求拒绝',
				desc: '您没有当前操作权限',
				onClose: function() {
					errNumber = 0
				}
			});
		}
	} else if (error == 404) {
		if (errNumber == 1) {
			that.$Notice.error({
				title: '访问数据有误',
				desc: '请联系管理员进行处理',
				onClose: function() {
					errNumber = 0
				}
			});
		}
	} else if (error == 401) { //forbiden
		if (errNumber == 1) {
			that.$Notice.error({
				title: '用户未登陆',
				desc: '请重新登陆该系统',
				onClose: function(errNumber) {
					errNumber = 0
				}
			});
		}
	} else if (error == 406) { //forbiden
		if (errNumber == 1) {
			that.$Notice.error({
				desc: "请先配置审批节点审批人信息",
				duration: 5,
				onClose: function() {
					errNumber = 0
				}
			});
		}
	} else if (error == 429) { //forbiden
		if (errNumber == 1) {
			that.$Notice.error({
				title: '操作异常',
				desc: "操作太快，请稍后再试",
				duration: 5,
				onClose: function() {
					errNumber = 0
				}
			});
		}
	} else {
		if (errNumber == 1) {
			that.$Notice.error({
				title: '网络异常',
				desc: '请联系管理员进行处理',
				onClose: function() {
					errNumber = 0
				}
			});
		}
	}
};

function startLoading(that) {

	that.$Loading.config({
		color: '#5cb85c',
		failedColor: '#f0ad4e',
		height: 4
	});
	that.$Loading.start();
}

function startSpin(that) {
	that.$Spin.show({
		render: (h) => {
			return h('div', [
				h('Icon', {
					'class': 'demo-spin-icon-load',
					props: {
						type: 'ios-loading',
						size: 60
					}
				}),
				h('div', 'Loading...')
			])
		}
	});
}
// cache默认时间为60s
const request = extend({
	timeout: 30000,
});
let get = function({
	spin,
	url,
	that,
	params,
	cache,
	cacheTime,
	success
}, ) {
	if (spin == 1) {
		startSpin(that);
	} else {
		startLoading(that);
	}
	if (cacheTime != undefined && typeof cacheTime === 'number') {
		cacheTime = cacheTime * 1000
	}
	request.get(url, {
		params: params,
		useCache: cache,
		ttl: cacheTime
	}, ).then(function(response) {
		//console.log(response);
		success(response)
		if (spin == 1) {
			that.$Spin.hide();
		} else {
			that.$Loading.finish();
		}
	}).catch(function(error) {
		console.log("----error----->", error.data)
		console.log(error)
		errNumber++
		dealError(error, that)

	});
	// Axios.get(url,{
	//   params: params
	// })
	// .then(response => {
	//   success(response.data);
	//   // setTimeout(() => {
	//   if(spin == 1){
	//     that.$Spin.hide();
	//   }else{
	//     that.$Loading.finish();
	//   }
	//   // }, 20000);
	//   // that.$Loading.finish();
	//   // that.$Message.info('知识条目已成功修改');
	// })
	// .catch(error => {
	//   dealError(error,that)
	// });
};

let post = function({
	spin,
	url,
	data,
	that,
	success,
	err
}) {
	if (spin == 1) {
		startSpin(that);
	} else {
		startLoading(that);
	}
	Axios.post(url, data, ).then(response => {
		success(response.data);
		if (spin == 1) {
			that.$Spin.hide();
		} else {
			that.$Loading.finish();
		}
	}).catch((error, config, code, request, response) => {
		console.log("error---->", error, config, code, request, response)
		errNumber++
		dealError(error, that)
		err()
	});
};

let put = function({
	spin,
	url,
	data,
	that,
	success,
	err
}) {
	if (spin == 1) {
		startSpin(that);
	} else {
		startLoading(that);
	}
	Axios.put(url, data).then(response => {
		success(response.data);
		if (spin == 1) {
			that.$Spin.hide();
		} else {
			that.$Loading.finish();
		}
	}).catch(error => {
		errNumber++
		dealError(error, that)
		err()
	});
};

let del = function({
	spin,
	url,
	data,
	that,
	success
}) {
	if (spin == 1) {
		startSpin(that);
	} else {
		startLoading(that);
	}
	Axios.delete(url, {
			data: data
		})
		.then(response => {
			success(response.data);
			if (spin == 1) {
				that.$Spin.hide();
			} else {
				that.$Loading.finish();
			}
		})
		.catch(error => {
			errNumber++
			dealError(error, that)
		});
};

export default {
	get: get,
	post: post,
	del: del,
	put: put
}
